import Express from "express";
import "./hbh_reptileChild.js";
// import path from "path"
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import RedisClient from "./redis.js";
import { expressjwt } from "express-jwt";
import cors from "cors";
import multer from "multer";
import router from "./router/index.js";
import "./router/files.js";
const secretKey = "itheima No1 ^_^";
var port = 9000;
var hostnameIp = "0.0.0.0";
var server = Express();
server.use(cors());
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
server.use("/public", Express.static(join(__dirname, "public")));
server.use(Express.json(), (req, res, next) => {
  next();
});
server.use(Express.urlencoded({ extended: false }), (req, res, next) => {
  next();
});
server.use(multer().array(), (req, res, next) => {
  next();
});
server.use(
  expressjwt({ secret: secretKey, algorithms: ["HS256"] }).unless({
    path: [/^\/api\//, /^\/app\//, /^\/public\//],
  })
);
server.use(router);
server.use((err, req, res, next) => {
  // console.log(err);
  if (err.name === "UnauthorizedError") {
    res.status(401);
    return res.send({
      code: 401,
      msg: "无效的令牌",
    });
  }
  res.status(500);
  res.send({
    code: 500,
    msg: err.toString(),
  });
});

server.listen(port,hostnameIp,function () {
  console.log("服务启动成功!");
});
// let client = new RedisClient();

// client
//   .connect()
//   .then(async () => {
//     client
//       .executeCommand("set", "hbh", "胡炳华2")
//       .then((res) => {
//         console.log(res);
//       })
//       .catch((err) => {
//         console.log(err);
//       })
//       .finally(async () => {
//         let res = await client.disrealseRedis();
//         console.log(res);
//       });
//   })
//   .catch((err) => {
//     console.log(err);
//   });
